soda()
	._page('iam_platform')
	._module(['tableMerge', 'xmSelect'])
	._api('/v1/iam/account')
	._query(['realName^like^', 'mobile^like^', 'authType^eq^-1'])
	._orderBy(['createTime^desc'])
	._run((unit) => {
		// Api
		unit.api['list'] = '/v1/iam/user/application/platform/account/list';
		unit.api['role'] = '/v1/iam/role/application';
		unit.api['app'] = '/v1/iam/account/application';
		
		// 表单配置
		unit.templateForm['verify'] = ["mobile", "realName"];
		unit.templateForm['items'] = [
			{code: 1, name:'mobile', title:'电话号码', placeholder:'请输入电话号码', attr: 'maxlength=20'},
			{code: 1, name:'realName', title:'真实姓名', placeholder:'请输入真实姓名', attr: 'maxlength=10'},
			{code: 2, name:'gender', title:'性别', options: unit.globalConstant['gender']},
			{code: 1, name:'email', title:'电子邮件', placeholder:'请输入电子邮件', attr: 'maxlength=50'}
		];
		
		// 表格基础配置
		unit.templateTable['url'] = unit.api['list'];
		unit.templateTable['cols'] = [
			{fixed: 'left', merge: true, field: 'mobile', title: '授权手机', minWidth: 100},
			{fixed: 'left', field: 'userType', title: '账号类型', minWidth: 100},
			{fixed: 'left', field: 'authType', title: '登录类型', minWidth: 100},
			{fixed: 'left', field: 'authAccount', title: '登录号', minWidth: 120},
			{field: 'realName', title: '姓名', minWidth: 120},
			{field: 'gender', title: '性别'},
			{field: 'openId', title: 'openId', minWidth: 120},
			{field: 'unionId', title: 'unionId', minWidth: 120},
			{field: 'role', title: '角色', minWidth: 120, templet(obj) {
			    if(obj.isAdmin == 1){
			        return "<span style='color:red;'>超管</span>";
			    }else{
					let arrs = [], r = '尚未配置';
					let roles = obj.role;
					if(roles.length > 0){
						$.each(roles, (i, d) => {
							arrs.push(d.name);
						})
						r = arrs.join(',')
					}
			        return r;
			    }
			}},
			{field: 'state', title: '状态',templet(obj) {
			    return unit.sdFormat['json']['get'](unit.globalConstant['enableState'], obj.state, 'value').title;
			}},
			{field: 'createTime', title: '注册时间', minWidth: 120, templet(obj) {
			    return unit.sdFormat['time']['t2t'](obj.createTime);
			}},
			{fixed: 'right', title: '',merge: true, field: 'mobile',minWidth: 80, templet(obj){
			    if(obj.isAdmin == 1){
			        // return "<span style='color:red;'>超管</span>";
			    }else{
			        return '<a data-flag="undefined" class="layui-btn layui-btn-xs" lay-event="role">配置角色</a>';
			    }
			}},
			{fixed: 'right', title: '操作', minWidth: 120, toolbar: unit.sysApplication['btns']['right']()},
		];
		unit.templateTable['bar'] = (bar) => {
			unit.templateDialog[bar.event]();
		}
		unit.templateTable['tool'] = (tool) => {
			unit.templateDialog[tool.event](tool.data);
		}
			
		// 表格查询配置
		unit.templateTable['search']['components'] = [
			{code:1, name:"name", title:"应用名称", placeholder:"请输入要查询的关键字"},
			{code:3, name:"state", title:"状态", options: unit.globalConstant['enableState']}
		]
		unit.templateTable['search']['submit'] = (data) => {
			unit.sdTable.reload(unit.el['table'], {
				where: unit.sdFormat['query'](unit.filter['vo'], data)
			});
		}
		
		//弹出层
		unit.templateDialog['form'] = (data) => {
			console.log('data', data)
			unit.sdDialog.form({
				title: data ? '修改' : "创建",
				height: '500px',
				init() {
					unit.sdForm['init']['form'](unit.templateForm, data);
				},
				opt(index) {
					// 获取form
					let form = unit.sdForm['get'](unit.el['form'], unit.templateForm['verify']);
					
					// 校验数据
					if (!form.check) {
						unit.sdDialog['tips']['warning'](form.message);
						return false;
					}
					
					// 数据处理
					let val = form.data;
					val['gender'] = val['gender'] || '保密';
					val['auth_account'] = val['mobile'];
			
					let param = {
						url: data ? unit.api['crud'] : unit.api['app'],
						data: val,
						tips: true,
						callback() {
							unit.sdDialog['close'](index);
							unit.sdTable['reload'](unit.el.table);
						}
					}
					// 操作
					data ? unit.sdHttp['put'](param, data.id) : unit.sdHttp['post'](param);
				}
			})
		}
		unit.templateDialog['create'] = unit.templateDialog['form'];
		unit.templateDialog['update'] = unit.templateDialog['form'];
		unit.templateDialog['delete'] = (data) => {
			unit.sdDialog.confirm({
				title: "是否要删除这条用户数据?",
				submit() {
					let param = {
						url: unit.api['app'],
						callback() {
							unit.sdTable['reload'](unit.el.table)
						}
					}
					unit.sdHttp['delete'](param, data.id);
				}
			});
		}
		unit.templateDialog['role'] = (data) => {
			let roleSelect = null;
			unit.sdDialog.page({
				title: "角色分配",
				height: '160px',
				skin: 'to-fix-select',
				url: '/components/iam/user/platform_role.html',
				init() {
					let param = {
					    url: unit.api['role'],
					    callback(d) {
					        let $arr = [];
					        if(d.length > 0){
					            for(let i=0;i<d.length;i++){
					                let model = d[i];
					                let obj = {'name': model['name'], 'value': model['id']};
					                $arr.push(obj);
					            }
								console.log(unit.xmSelect)
					            roleSelect = unit.xmSelect.render({
									el: '#user-select-role',
									data: $arr
								});
								
					            if(data.role){
					               roleSelect.setValue(data.role);
					            }
					        }
					    }
					}
					unit.sdHttp['get'](param);
				},
				opt(index) {
					let vals = roleSelect.getValue();
					
					let param = {
					    url: unit.api['role'],
					    data: vals,
					    tips: true,
					    callback: function (d) {
					        unit.sdDialog['close'](index);
					        unit.sdTable['reload'](unit.el.table);
					    }
					}
					unit.sdHttp['put'](param, data.userId);
				}
			})
		}
	
		unit.sdTable['render'](unit.templateTable, unit.tableMerge);
	})